package com.zoltu.GoogleAuthenticator;

import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddAccountActivity extends Activity
{
	protected Button mLoginButton;
	protected Button mCancelButton;
	protected EditText mUsernameEditText;
	protected EditText mPasswordEditText;
	
	@Override protected void onCreate(Bundle pSavedInstanceState)
	{
		super.onCreate(pSavedInstanceState);
		
		/* Set the layout file to use for this Activity. */
		setContentView(R.layout.add_account_activity);
		
		/* Get handles to the various views in the Activity. */
		mLoginButton = (Button) findViewById(R.id.login_button);
		mCancelButton = (Button) findViewById(R.id.cancel_button);
		mUsernameEditText = (EditText) findViewById(R.id.username_edit_text);
		mPasswordEditText = (EditText) findViewById(R.id.password_edit_text);
		
		/* Setup the login button to call onClick on this class when it's clicked. */
		mLoginButton.setOnClickListener(new LoginClickListener());
		mCancelButton.setOnClickListener(new CancelClickListener());
	}
	
	private class LoginClickListener implements OnClickListener
	{
		@Override public void onClick(View pView)
		{
			final AccountAuthenticatorResponse lAccountAuthenticatorResponse = getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
			final String lUsername = mUsernameEditText.getText().toString();
			final String lPassword = mPasswordEditText.getText().toString();
			
			/* Now that we have the username / password from the user, spawn a new service that will attempt authentication. */
			Intent lIntent = new Intent();
			lIntent.setClass(AddAccountActivity.this, AuthenticatorService.class);
			lIntent.setAction(AuthenticatorService.ACTION_ADD_USER);
			lIntent.putExtra(AuthenticatorService.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, lAccountAuthenticatorResponse);
			lIntent.putExtra(AuthenticatorService.KEY_USERNAME, lUsername);
			lIntent.putExtra(AuthenticatorService.KEY_PASSWORD, lPassword);
			
			/* Send off the intent. */
			startService(lIntent);
			
			/* Close the activity. */
			AddAccountActivity.this.finish();
		}
	}
	
	private class CancelClickListener implements OnClickListener
	{
		@Override public void onClick(View pView)
		{
			final AccountAuthenticatorResponse lAccountAuthenticatorResponse = getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
			final String lErrorMessage = getResources().getString(R.string.error_user_canceled);
			
			/* Build the intent to cancel the authentication. */
			Intent lIntent = new Intent();
			lIntent.setClass(AddAccountActivity.this, AuthenticatorService.class);
			lIntent.setAction(AuthenticatorService.ACTION_CANCEL_AUTHENTICATION);
			lIntent.putExtra(AuthenticatorService.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, lAccountAuthenticatorResponse);
			lIntent.putExtra(AuthenticatorService.KEY_ERROR_CODE, AccountManager.ERROR_CODE_CANCELED);
			lIntent.putExtra(AuthenticatorService.KEY_ERROR_MESSAGE, lErrorMessage);
			
			/* Send off the intent. */
			startService(lIntent);
			
			/* Close the activity. */
			AddAccountActivity.this.finish();
		}
	}
	
//	@Override public void AuthenticationSuccss(String pAuthToken)
//	{
//		Intent lIntent = getIntent();
//		
//		/* Create an account based on the provided information. */
//		Account lAccount = new Account(mUsernameEditText.getText().toString(), "com.google");
//		
//		/* Get access to the AccountManager so we can add the account to the accounts store. */
//		AccountManager lAccountManager = AccountManager.get(this);
//		
//		/* Add the account we just authenticated to the Android account store. */
//		if (lAccountManager.addAccountExplicitly(lAccount, mPasswordEditText.getText().toString(), new Bundle()))
//		{
//			/* Build the bundle to send as a response to the original Authenticator request. */
//			Bundle lBundle = new Bundle();
//			lBundle.putString(AccountManager.KEY_ACCOUNT_NAME, lAccount.name);
//			lBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, lAccount.type);
//			
//			/* If the original call requested an auth token then provide one. */
//			if (lIntent.getExtras().getString(Authenticator.KEY_AUTH_TOKEN_TYPE) != null)
//			{
//				lBundle.putString(AccountManager.KEY_AUTHTOKEN, pAuthToken);
//			}
//			
//			/* Setup the result bundle to get sent back to the Authenticator. */
//			setAccountAuthenticatorResult(lBundle);
//		}
//		
//		/*
//		 * This causes the activity to complete (and close) and sends the bundle we just built back to the Authenticator
//		 * (or an error bundle if adding the account failed).
//		 */
//		finish();
//	}
//	
//	@Override public void AuthenticationFailure(Bundle pErrorBundleForResponse)
//	{
//		int lErrorCode = pErrorBundleForResponse.getInt(AccountManager.KEY_ERROR_CODE);
//		switch (lErrorCode)
//		{
//			/* If the error is failed credentials then let the user attempt to login again. */
//			case Authenticator.ERROR_CODE_BAD_CREDENTIALS:
//			{
//				/* Reset the buttons so they can attempt to authenticate again. */
//				mLoginButton.setVisibility(Button.VISIBLE);
//				mAuthenticatingFeedbackButton.setVisibility(Button.GONE);
//				
//				/* Clear the password field. */
//				mPasswordEditText.setText("");
//				
//				/* TODO: Alert the user that authentication failed somehow. */
//
//				break;
//			}
//			
//			/* If the error is anything else then end the Activity and tell the Authenticator about the problem. */
//			default:
//			{
//				setAccountAuthenticatorResult(pErrorBundleForResponse);
//				finish();
//				break;
//			}
//		}
//	}
}
